#include "yb_form_hj.h"
#include "ui_yb_form_hj.h"
#include "pub_var.h"

#include <QSettings>
#include <QMessageBox>
#include <QDebug>
#include <QSqlError>
#include <QSqlRecord>


Form_ybhj::Form_ybhj(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_ybhj)
{
    ui->setupUi(this);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    //恢复当前窗口位置
    QRect rct = setIni.value("windowState/ybhj").toRect();
    this->setGeometry(rct);
    pub::DEBUG = (setIni.value("system/debug").toString() == "1" ? true : false);

    ui->le_database_ip->setText(setIni.value("database/m_HostName").toString());

    if(pub::DEBUG)
    {
        ui->rbTemp_2->setChecked(true);
    }else{
        ui->rbNorm->setChecked(true);
    }

    //数据库连接
    qrm = new QSqlQuery(pub::db_m);
    qro = new QSqlQuery(pub::db_o);

    bool db_OK = true;

    if(!pub::get_db_connect("a"))
    {
        qDebug()<<"Form_ybhj, connection database return error.\n";
        //return;
        db_OK = false;
    }


    on_rbTemp_clicked();
}

Form_ybhj::~Form_ybhj()
{
    delete ui;
}

void Form_ybhj::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/ybhj", QVariant(this->geometry()));
    destroyW("ybhj");

}

void Form_ybhj::on_pbExit_clicked()
{
    this->close();
}

void Form_ybhj::on_pbSave_clicked()
{
    QString mark ;
    if(ui->rbNormal->isChecked())
    {
        mark = "normal";
    }
    else if(ui->rbTemp->isChecked())
    {
        mark = "temp";
    }
    else
    {
        QMessageBox::information(this,"注意","状态选择错误！");
        return;
    }
    sq = " update com_hospitalinfo set "
         " hos_name = '" + ui->le_hos_name->text()
            + "', new_hos_ybbh = '" + ui->le_new_hos_ybbh->text()
            + "', new_hos_dzpz_ip = '" + ui->le_new_hos_dzpz_ip->text()
            + "', new_hos_ybjy_ip = '" + ui->le_new_hos_ybjy_ip->text()
            + "', new_hos_insuplc = '" + ui->le_new_hos_insuplc->text()
            + "', new_hos_secretcode = '" + ui->te_new_hos_secretcode->toPlainText()

            + "', zj_push_ip = '" + ui->le_zj_push_ip->text()
            + "', zj_app_id = '" + ui->le_app_id->text()
            + "', zj_key_pub_app = '" + ui->te_key_pub_app->toPlainText()
            + "', zj_key_private_app = '" + ui->te_key_private_app->toPlainText()
            + "', zj_key_pub_pub = '" + ui->te_key_pub_pub->toPlainText()
            + "', zj_key_sm4 = '" + ui->le_key_sm4->text()
            + "' "
              " where mark = '" + mark + "'";


    qrm->exec(sq);

    QSettings setIni("clin.ini", QSettings::IniFormat);

    if(pub::DEBUG)
    {
        setIni.setValue("system/debug", "1");
    }else{
        setIni.setValue("system/debug", "0");
    }

    setIni.setValue("database/o_HostName", ui->le_database_ip->text());
    setIni.setValue("database/m_HostName", ui->le_database_ip->text());

    QMessageBox::information(this,"注意","配置保存成功！");
}

void Form_ybhj::on_rbTemp_clicked()
{
    QString mark ;
    if(ui->rbNormal->isChecked())
    {
        mark = "normal";
    }
    else if(ui->rbTemp->isChecked())
    {
        mark = "temp";
    }
    else
    {
        QMessageBox::information(this,"注意","状态选择错误！");
        return;
    }

    sq = "select * from com_hospitalinfo where mark = '" + mark + "'";

    qrm->exec(sq);
    qrm->next();
    ui->le_hos_name->setText(qrm->value("hos_name").toString());
    ui->le_new_hos_ybbh->setText(qrm->value("new_hos_ybbh").toString());
    ui->le_new_hos_dzpz_ip->setText(qrm->value("new_hos_dzpz_ip").toString());
    ui->le_new_hos_ybjy_ip->setText(qrm->value("new_hos_ybjy_ip").toString());
    ui->le_new_hos_insuplc->setText(qrm->value("new_hos_insuplc").toString());
    ui->te_new_hos_secretcode->setText(qrm->value("new_hos_secretcode").toString());

    ui->le_zj_push_ip->setText(qrm->value("zj_push_ip").toString());
    ui->le_app_id->setText(qrm->value("zj_app_id").toString());
    ui->le_key_sm4->setText(qrm->value("zj_key_sm4").toString());
    ui->te_key_pub_app->setText(qrm->value("zj_key_pub_app").toString());
    ui->te_key_private_app->setText(qrm->value("zj_key_private_app").toString());
    ui->te_key_pub_pub->setText(qrm->value("zj_key_pub_pub").toString());


}

void Form_ybhj::on_rbNormal_clicked()
{
    on_rbTemp_clicked();
}

void Form_ybhj::on_rbNorm_clicked()
{
    if(ui->rbNorm->isChecked())
    {
        pub::DEBUG = false;
    }
    else
    {
        pub::DEBUG = true;
    }

}

void Form_ybhj::on_rbTemp_2_clicked()
{
    on_rbNorm_clicked();
}
